package com.crm.dao;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.crm.entity.DeliveryChallan;
import com.crm.util.ApplicationContext;

public class DeliveryChallanDAO {
	protected final Log logger = LogFactory.getLog(getClass());
	static Transaction tx;
	public  void insert(DeliveryChallan dc) throws Exception {
		Session session =  ApplicationContext.getHiberSession().openSession();
		Transaction tx=null;
		try {
		     tx = session.beginTransaction();
			session.saveOrUpdate(dc);
			tx.commit();
			return ;
		}
		catch (Exception ex) {
		     if (tx!=null) tx.rollback();
		     throw ex;
		}
		finally {
		     session.close();
		}
		}
	public  Long getDCCount(int busid) throws Exception{
		Session session =  ApplicationContext.getHiberSession().openSession();
		try {
			Calendar currentDate = Calendar.getInstance();
			SimpleDateFormat formatter= 
			new SimpleDateFormat("yyyy-MM-dd");
			String dt = formatter.format(currentDate.getTime());
			String[] temp=dt.split("-");
			String year=temp[0];
			String querystr="select COUNT(*)+1 from DeliveryChallan where business_id=:busid";
			Query query=session.createQuery(querystr);
			query.setInteger("busid", busid);
			Object queryresult=query.uniqueResult();
			Long count=(Long) queryresult;
			return count;
		}
		catch (Exception ex) {
		     throw ex;
		}
		finally {
		     session.close();
		}
	}

}
